const { Sdk_Platform } = require("./constants")

module.exports = {
    get(key, from) {
        if (from) {
            let fromJson = cc.sys.localStorage.getItem(from)
            if (fromJson) {
                let Dict = JSON.parse(fromJson)
                if (Dict[key] != undefined) {
                    return Dict[key]
                }
            }
            return null
        }
        return cc.sys.localStorage.getItem(key)
    },
    getJson(key, from) {
        if (from) {
            let fromJson = cc.sys.localStorage.getItem(from)
            let Dict = JSON.parse(fromJson)
            if (Dict[key] != undefined) {
                return Dict[key]
            }
            return null
        }
        let data = cc.sys.localStorage.getItem(key)
        if (data) {
            return JSON.parse(data)
        }
        return null
    },
    set(key, value, from) {
        if (from) {
            let fromJson = cc.sys.localStorage.getItem(from)
            let Dict = {}
            if (fromJson) {
                Dict = JSON.parse(fromJson)
            }
            Dict[key] = value
            cc.sys.localStorage.setItem(from, JSON.stringify(Dict))
        } else {
            cc.sys.localStorage.setItem(key, value)
        }
    },
    setJson(key, value, from) {
        if (from) {
            let fromJson = cc.sys.localStorage.getItem(from)
            let Dict = {}
            if (fromJson) {
                Dict = JSON.parse(fromJson)
            }
            Dict[key] = value
            cc.sys.localStorage.setItem(from, JSON.stringify(Dict))
        } else {
            cc.sys.localStorage.setItem(key, JSON.stringify(value))
        }
    },
    click(v) {
        let count = this.get(v, 'or')    // 操作记录
        if (count) {
            count += 1
        } else {
            count = 1
        }
        this.set(v, count, 'or')
    },
    saveOperation() {
        let info = this.getJson('user')
        let OprationRecord = this.getJson('or')
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                if (OprationRecord) {
                    let upload_info = {
                        uid: info.uid,
                        operation: OprationRecord,
                    }
                    cloud.callFunction({
                        name: 'addOperation',
                        success: res => {
                            cc.sys.localStorage.removeItem("or")
                        },
                        data: upload_info
                    })
                }
                break
            case Sdk_Platform.VIVO:
                if (OprationRecord) {
                    let upload_info = {
                        uid: info.uid,
                        operation: OprationRecord,
                    }
                    cloud.callFunction({
                        name: 'addOperation',
                        data: upload_info
                    }).then(res => {
                        cc.sys.localStorage.removeItem("or")
                    }).catch(err => {
                    })
                }
                break
            case Sdk_Platform.OPPO:
                if (OprationRecord) {
                    let upload_info = {
                        uid: info.uid,
                        operation: OprationRecord,
                    }
                    cloud.callFunction({
                        name: 'addOperation',
                        data: upload_info
                    }).then(res => {
                        cc.sys.localStorage.removeItem("or")
                    }).catch(err => {
                    })
                }
                break
        }
    }
}
